<?php
	session_start();
	require 'libs/seguridad.php';
	require 'libs/smarty-2.6.22/Smarty.class.php';
	require 'clases/Usuario.class.php';
	include 'libs/db.php';
	
	$smarty = new Smarty;
	$usuario = new Usuario($db,$_SESSION['id_usu']);
	$usuario->getDatos();
	$foto = $usuario->getFoto();
	

	//Verificar si es un archivo con extensión: JPEG, GIF, PNG
	if($_FILES['txtFoto']['type'] == 'image/jpeg' || $_FILES['txtFoto']['type'] == 'image/pjpeg'){
		$_FILES['txtFoto']['name'] = $_SESSION['id_usu'].'.jpeg';
	} else {
		if($_FILES['txtFoto']['type'] == 'image/png'){
			$_FILES['txtFoto']['name'] = $_SESSION['id_usu'].'.png';
		} else {
			if($_FILES['txtFoto']['type'] == 'image/gif') {
				$_FILES['txtFoto']['name'] = $_SESSION['id_usu'].'.gif';
			} else {
				die($_FILES['txtFoto']['type']);
			}
		}
	}
	
	$upfile = 'images/profiles/'.$_FILES['txtFoto']['name'];
	
	if (is_uploaded_file($_FILES['txtFoto']['tmp_name'])) {
		if (!move_uploaded_file($_FILES['txtFoto']['tmp_name'], $upfile))
			die('Could not move file to destination directory.');
	} else {
		die('Problem: Possible file upload attack. Filename: '.$nuevaFoto);
	}
	
	$usuario->setFoto($_FILES['txtFoto']['name']);
	if(!$usuario->actualizar())
		die('No se pudo actualizar.');
	if($foto != "noPicture.jpg")
		@unlink('images/profiles/'.$foto); 
	Header("Location: home.php"); 
?>